import md5 from "js-md5"
import { getNowImgPoint } from "./mathFun"
export default class rectangleTag {
    //id
    id = "";
    //颜色
    color = "yellow";
    //线条宽度
    width = 2;
    //显示
    show = true;
    //点位
    position = [];
    //绘制时的点位
    drawPosition = [];
    //边界
    border = [];
    //创建时间
    createdT = null;
    //高亮
    highLight = false;
    //标注中文
    zhcn = "";
    //标注藏文
    zy = "";
    //标题
    title = "";
    //绘制标题
    showTitle = false;

    constructor(PointL, PointR, position = null, taggingConfig) {
        this.position[0] = PointL
        this.position[1] = PointR
        this.color = taggingConfig.color
        this.width = taggingConfig.width
        if (taggingConfig.objid) {
            this.id = taggingConfig.objid
        } else {
            this.id = md5(new Date().getTime().toString())
        }
        this.zhcn = taggingConfig.zhcn ? taggingConfig.zhcn : ""
        this.zhcn = taggingConfig.zy ? taggingConfig.zy : ""
        this.title = taggingConfig.title ? taggingConfig.title : ""
        this.createdT = taggingConfig.createdT ? taggingConfig.createdT : 0
        if (typeof position == "object") {
            let [x1, y1] = getNowImgPoint(PointL.X, PointL.Y, position)
            let [x2, y2] = getNowImgPoint(PointR.X, PointR.Y, position)
            this.drawPosition = [x1, y1, x2 - x1, y2 - y1]
            if (x1 > x2) {
                [x1, x2] = [x2, x1]
            }
            if (y1 > y2) {
                [y1, y2] = [y2, y1]
            }
            this.border = [x1, y1, x2, y2]
        }
    }

    //重设点位信息
    resetPoint(x1, y1, x2, y2, position) {
        this.position[0].X = x1
        this.position[0].Y = y1
        this.position[1].X = x2
        this.position[1].Y = y2
        if (typeof position == "object") {
            let [xx1, yy1] = getNowImgPoint(x1, y1, position)
            let [xx2, yy2] = getNowImgPoint(x2, y2, position)
            this.drawPosition = [xx1, yy1, xx2 - xx1, yy2 - yy1]
            if (xx1 > xx2) {
                [xx1, xx2] = [xx2, xx1]
            }
            if (yy1 > yy2) {
                [yy1, yy2] = [yy2, yy1]
            }
            this.border = [xx1, yy1, xx2, yy2]
        }
    }

    //拖拽时自动重设点位信息
    dragAutoReset(position) {
        let x1 = this.position[0].X
        let y1 = this.position[0].Y
        let x2 = this.position[1].X
        let y2 = this.position[1].Y
        if (typeof position == "object") {
            let [xx1, yy1] = getNowImgPoint(x1, y1, position)
            let [xx2, yy2] = getNowImgPoint(x2, y2, position)
            this.drawPosition = [xx1, yy1, xx2 - xx1, yy2 - yy1]
            if (xx1 > xx2) {
                [xx1, xx2] = [xx2, xx1]
            }
            if (yy1 > yy2) {
                [yy1, yy2] = [yy2, yy1]
            }
            this.border = [xx1, yy1, xx2, yy2]
        }
    }

    //计算点是否在标注内
    inTag(point) {
        return point.X >= this.border[0] && point.X <= this.border[2] && point.Y >= this.border[1] && point.Y <= this.border[3];
    }

    get positionP() {
        return [this.position[0].X, this.position[0].Y, this.position[1].X, this.position[1].Y]
    }

}